package kr.saeildamil.androidstudty.ch19;

import kr.saeildamil.androidstudty.R;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.TextView;

public class MessageObtain extends Activity {
	int mMainValue=0;
	TextView mMain;
	TextView mBack;
	BackThread thread;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ch19_threadtest);
		
		mMain=(TextView) findViewById(R.id.ch19_threadtest_mainvalue);
		mBack=(TextView) findViewById(R.id.ch19_threadtest_backvalue);
		
		thread=new BackThread(mHandler);
		thread.setDaemon(true);
		thread.start();
	}
	public void mOnClick(View v){
		mMainValue++;
		mMain.setText("MainValue : "+mMainValue);
	}
	
	Handler mHandler=new Handler(){
		public void handleMessage(android.os.Message msg) {
			if(msg.what==0){
				mBack.setText("Back Value :"+msg.arg1);
			}
		}
	};
	class BackThread extends Thread {
		int mBackValue=0;
		Handler mHandler;
		
		BackThread(Handler handler){
			mHandler=handler;
		}
		@Override
		public void run() {
			super.run();
			while (true) {
				mBackValue++;
				Message msg=Message.obtain(mHandler,0, mBackValue,0);
				mHandler.sendMessage(msg);
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	} 
}
